package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShopCartMapper;
import com.sky.service.ShopCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShopCartServiceImpl implements ShopCartService {

    @Autowired
    private ShopCartMapper shopCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void addCart(ShoppingCartDTO shoppingCartDTO) {
        //1. 创建pojo并拷贝数据到pojo
        ShoppingCart cart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, cart);
        cart.setUserId(BaseContext.getCurrentUserId());

        //2. 用pojo里面的数据查询购物车数据
        /**
         * shoppingCartDTO: dish_id: 1 dishFlavor：微辣
         *  select * from shop_cart where user_id = 1 and dish_id = 1 and dishflavor = '微辣'
         */
        List<ShoppingCart> list =  shopCartMapper.list(cart);

        //3. 判断是否查询到了购物车的数据 如果查询到了则修改购物车里面的数据
        if(null != list && list.size() == 1){
            ShoppingCart shoppingCart = list.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);

            shopCartMapper.update(shoppingCart);
        }else{
            //4. 如果没有查询到 则判断新增的是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            if(null != dishId){
                //5. 如果是菜品 则查询菜品的详细信息 并封装到购物车数据中
                Dish dish = dishMapper.getById(dishId);
                cart.setName(dish.getName());
                cart.setImage(dish.getImage());
                cart.setAmount(dish.getPrice());
            }else{
                //6. 如果是套餐则查询套餐的详细信息 封装到购物车数据中
                Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                cart.setName(setmeal.getName());
                cart.setImage(setmeal.getImage());
                cart.setAmount(setmeal.getPrice());
            }

            cart.setNumber(1);
            cart.setCreateTime(LocalDateTime.now());

            //7. 新增购物车数据
            shopCartMapper.insert(cart);
        }


    }

    /**
     * 查询购物车数据
     *
     * @return
     */
    @Override
    public List<ShoppingCart> listCart() {
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(BaseContext.getCurrentUserId()).build();
        return shopCartMapper.list(shoppingCart);
    }

    /**
     * 清空购物车数据
     */
    @Override
    public void deleteAllByUserId() {
        shopCartMapper.deleteAllByUserId(BaseContext.getCurrentUserId());
    }
}
